var bearcat = require("bearcat")

var rankHandler = function(app) {
	this.app = app;
	this.sessionService = this.app.get('sessionService')
  this.rankServer = this.app.get('rankServer')
};
//改变日排行榜分数
rankHandler.prototype.changeDayRank = function(msg, session, next) {
  var uid = session.get("uid")
  if(!uid){
    next(null,{flag : false,msg : "未登录"})
    return
  }
  var ustr = msg.ustr
  var gameType = msg.gameType
  var score = msg.score
  this.rankServer.changeDayRank(ustr,gameType,score,function(flag,msg) {
    next(null,{flag : flag,msg : msg})
  })
}
//获取日排行榜
rankHandler.prototype.getDayRank = function(msg, session, next) {
  var uid = session.get("uid")
  if(!uid){
    next(null,{flag : false,msg : "未登录"})
    return
  }
  var gameType = msg.gameType
  var count = msg.count
  this.rankServer.getDayRank(gameType,count,function(flag,msg) {
    next(null,{flag : flag,msg : msg})
  })
}
//获取昨日排行榜
rankHandler.prototype.getYesterdayRank = function(msg, session, next) {
  var uid = session.get("uid")
  if(!uid){
    next(null,{flag : false,msg : "未登录"})
    return
  }
  var gameType = msg.gameType
  var count = msg.count
  this.rankServer.getYesterdayRank(gameType,count,function(flag,msg) {
    next(null,{flag : flag,msg : msg})
  })
}
//改变总排行榜分数
rankHandler.prototype.changeDurableRank = function(msg, session, next) {
  var uid = session.get("uid")
  if(!uid){
    next(null,{flag : false,msg : "未登录"})
    return
  }
  var ustr = msg.ustr
  var gameType = msg.gameType
  var score = msg.score
  this.rankServer.changeDurableRank(ustr,gameType,score,function(flag,msg) {
    next(null,{flag : flag,msg : msg})
  })
}
//获取总排行榜
rankHandler.prototype.getDurableRank = function(msg, session, next) {
  var uid = session.get("uid")
  if(!uid){
    next(null,{flag : false,msg : "未登录"})
    return
  }
  var gameType = msg.gameType
  var count = msg.count
  this.rankServer.getDurableRank(gameType,count,function(flag,msg) {
    next(null,{flag : flag,msg : msg})
  })
}
module.exports = function(app) {
  return bearcat.getBean({
  	id : "rankHandler",
  	func : rankHandler,
  	args : [{
  		name : "app",
  		value : app
  	}],
  	props : [{
  		name : "redisDao",
  		ref : "redisDao"
  	}]
  })
};